package com.doctor.windflower_doctor.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.adapter.ShitAdapter;
import com.doctor.windflower_doctor.entity.ShitBeen;
import com.doctor.windflower_doctor.myuser.tools.HttpclientHelper;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.util.LogUtils;
import com.doctor.windflower_doctor.util.UserComparator;

public class ShitActivity extends BaseActivity implements OnClickListener, Constent{

	private ImageView iv_backs, img1, img2, img3, img4, img5, img6, img7;
	private TextView tv_title, date_text1, date_text2, date_text3, date_text4,
			date_text5, date_text6, date_text7;
	private List<ShitBeen> shit_List;

	private ListView listView;
	private UserComparator userComparator = new UserComparator();
	private ShitAdapter adapter;
	private ArrayList<String> monthList;
	private List<Integer> shitList;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		iv_backs.setOnClickListener(this);

	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		new InspectionTask().execute(RECORD_URL_ONLINE+SHIT+SystemApplication.getInstance().doctorId);
		adapter = new ShitAdapter(ShitActivity.this);

		shitList = new ArrayList<Integer>();

		tv_title = (TextView) findViewById(R.id.textView);
		iv_backs = (ImageView) findViewById(R.id.back_btn);
		listView = (ListView) findViewById(R.id.listView);
		listView.setAdapter(adapter);
		img1 = (ImageView) findViewById(R.id.img1);
		img2 = (ImageView) findViewById(R.id.img2);
		img3 = (ImageView) findViewById(R.id.img3);
		img4 = (ImageView) findViewById(R.id.img4);
		img5 = (ImageView) findViewById(R.id.img5);
		img6 = (ImageView) findViewById(R.id.img6);
		img7 = (ImageView) findViewById(R.id.img7);
		date_text1 = (TextView) findViewById(R.id.date_text1);
		date_text2 = (TextView) findViewById(R.id.date_text2);
		date_text3 = (TextView) findViewById(R.id.date_text3);
		date_text4 = (TextView) findViewById(R.id.date_text4);
		date_text5 = (TextView) findViewById(R.id.date_text5);
		date_text6 = (TextView) findViewById(R.id.date_text6);
		date_text7 = (TextView) findViewById(R.id.date_text7);
		tv_title.setText("通便记录");
		listView.setDividerHeight(0);

	}

	private void initDate() {
		shitList.clear();
		getDayList();
		if (shit_List != null)
			for (int j = 0; j < 7; j++) {
				shitList.add(0);
				for (int i = 0; i < shit_List.size(); i++) {
					if (getMonthDay(
							Long.valueOf(shit_List.get(i).getCreatedAt()))
							.equals(monthList.get(j))) {
						shitList.remove(j);
						if (shit_List.get(i).isPurge()) {
							shitList.add(1);
						} else {
							shitList.add(0);
						}

					}
				}

			}
		for (int i = 0; i < monthList.size(); i++) {

			switch (i) {
			case 0:
				date_text1.setText(monthList.get(i));
				if (shitList.get(i) == 1) {
					img1.setSelected(true);
				} else {
					img1.setSelected(false);
				}
				break;
			case 1:
				date_text2.setText(monthList.get(i));
				if (shitList.get(i) == 1) {
					img2.setSelected(true);
				} else {
					img2.setSelected(false);
				}
				break;
			case 2:
				date_text3.setText(monthList.get(i));
				if (shitList.get(i) == 1) {
					img3.setSelected(true);
				} else {
					img3.setSelected(false);
				}
				break;
			case 3:
				date_text4.setText(monthList.get(i));
				if (shitList.get(i) == 1) {
					img4.setSelected(true);
				} else {
					img4.setSelected(false);
				}
				break;

			case 4:
				date_text5.setText(monthList.get(i));
				if (shitList.get(i) == 1) {
					img5.setSelected(true);
				} else {
					img5.setSelected(false);
				}
				break;
			case 5:
				date_text6.setText(monthList.get(i));
				if (shitList.get(i) == 1) {
					img6.setSelected(true);
				} else {
					img6.setSelected(false);
				}
				break;
			case 6:
				date_text7.setText(monthList.get(i));
				if (shitList.get(i) == 1) {
					img7.setSelected(true);
				} else {
					img7.setSelected(false);
				}
				break;

			}
		}
	}

	@Override
	protected void onStart() {
		LogUtils.e("=======onStart========");
		// initDate();
		initBottomDate();
		super.onStart();

	}

	private void initBottomDate() {
		if (adapter.getCount() > 0)

			adapter.addList(shit_List);
	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.shit_record;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;
		}

	}

	@SuppressLint("SimpleDateFormat")
	private ArrayList<String> getDayList() {
		Long h = Long.parseLong("86400000");
		Date date = new Date();
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Long r1 = cal.getTimeInMillis();
		Long t1 = r1.longValue();
		monthList = new ArrayList<String>();
		for (Long i = (long) 6; i >= 0; i--) {
			Date dateC = new Date(t1 - i * h);
			monthList.add(format1.format(dateC).substring(
					format1.format(dateC).indexOf("-") + 1));
			LogUtils.e("=======mothlist========"
					+ format1.format(dateC).substring(
							format1.format(dateC).indexOf("-") + 1));
		}
		return monthList;
	}

	@SuppressLint("SimpleDateFormat")
	String getMonthDay(long dateStr) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
		Date date = new Date(dateStr);
		return dateFormat.format(date);
	}

	class InspectionTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return HttpclientHelper.getAddress(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {

				shit_List = json(result);
				adapter.addList(shit_List);
				Collections.sort(shit_List, userComparator);
				initDate();
			} else {
				Toast.makeText(ShitActivity.this, "请检查网络状态", Toast.LENGTH_SHORT)
						.show();
			}
		}
	}

	public List<ShitBeen> json(String json) {
		JSONObject jsonObject;
		List<ShitBeen> list = new ArrayList<ShitBeen>();
		try {
			jsonObject = new JSONObject(json);

			if (jsonObject.getBoolean("success")) {

				JSONObject jsonObject2 = jsonObject.getJSONObject("data");
				JSONArray jsonArray = jsonObject2.getJSONArray("lists");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject3 = jsonArray.getJSONObject(i);
					ShitBeen been = new ShitBeen();

					been.setCreatedAt(jsonObject3.getString("createdAt"));
					been.setPurge(jsonObject3.getBoolean("purge"));
					list.add(been);

				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}
